package com.okami.web.order;

import com.okami.domain.order.Order;
import com.okami.domain.order.OrderRepository;
import com.okami.domain.order.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author: TanJingyu
 * @create: 2020-12-05 17:46
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderRepository orderRepository;
    private final OrderService orderService;
    private Logger logger = LoggerFactory.getLogger(getClass());

    public OrderController(OrderRepository orderRepository, OrderService orderService) {
        this.orderRepository = orderRepository;
        this.orderService = orderService;
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<GetOrderResponse> getOrder(@PathVariable Integer orderId) {
        Optional<Order> order = orderRepository.findById(orderId);
        return order.map(o -> new ResponseEntity<>(makeGetOrderResponse(o), HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    private GetOrderResponse makeGetOrderResponse(Order order) {
        return new GetOrderResponse(order.getId(), order.getRemarks(), order.getCreatedTime(), order.getLocalDateTime(), order.getState().toString(), order.getPaymentInformation());
    }

    @PostMapping
    public ResponseEntity<CreateOrderResponse> craeteOrder(@RequestBody CreateOrderRequest createOrderRequest) {

//        orderRepository.save()

        return ResponseEntity.ok(new CreateOrderResponse());
    }
}
